/**
 * Notes: 项目管理模块 后台管理服务
 * Date: 2023-07-01 10:20:00 
 */

const BaseProjectAdminService = require('./base_project_admin_service.js');
const util = require('../../../../framework/helpers/util.js');
const dataUtil = require('../../../../framework/helpers/data_util.js');
const cloudUtil = require('../../../../framework/cloud/cloud_util.js');
const timeUtil = require('../../../../framework/helpers/time_util.js');
const cloudBase = require('../../../../framework/cloud/cloud_base.js');

class AdminProjectService extends BaseProjectAdminService {

	/** 
	 * 清理所有项目数据
	 */
	async clearProjects() {
		// this.AppError('此功能暂未开放，如需使用请联系管理员');
		
		const cloud = cloudBase.getCloud();
		const db = cloud.database();
		const _ = db.command;
		
		// 查询条件 - 必须使用明确的条件而不是空对象
		const condition = {
			_id: _.exists(true)  // 查询所有存在_id字段的记录，即所有记录
		};
		
		// 清理所有项目相关数据
		try {
			// === 常规项目表 ===
			// 清理oa_project表 (常规项目表)
			const projectCollection = db.collection('oa_project');
			const projectResult = await projectCollection.where(condition).remove();
			
			// 清理扩展项目表 (如果有)
			const projectExtCollection = db.collection('oa_project_ext');
			const projectExtResult = { deleted: 0 };
			try {
				const extResult = await projectExtCollection.where(condition).remove();
				projectExtResult.deleted = extResult.deleted || 0;
			} catch (e) {
				console.warn('[清理项目] oa_project_ext表可能不存在:', e);
			}
			
			// === 研发项目表 ===
			// 清理研发项目主表
			const researchProjectCollection = db.collection('research_project');
			const researchProjectResult = await researchProjectCollection.where(condition).remove();
			
			// 清理项目资源
			const resourceCollection = db.collection('research_project_resource');
			const resourceResult = await resourceCollection.where(condition).remove();
			
			// 清理项目历史
			const historyCollection = db.collection('research_project_state_history');
			const historyResult = await historyCollection.where(condition).remove();
			
			// 清理项目分支
			const branchCollection = db.collection('research_project_branch');
			const branchResult = await branchCollection.where(condition).remove();
			
			// 清理项目版本
			const versionCollection = db.collection('research_project_resource_version');
			const versionResult = await versionCollection.where(condition).remove();
			
			// 清理项目快照
			const snapshotCollection = db.collection('research_project_snapshot');
			const snapshotResult = await snapshotCollection.where(condition).remove();
			
			// === 项目任务表 ===
			// 清理项目任务
			const taskCollection = db.collection('oa_task');
			const taskResult = { deleted: 0 };
			try {
				const tResult = await taskCollection.where(condition).remove();
				taskResult.deleted = tResult.deleted || 0;
			} catch (e) {
				console.warn('[清理项目] oa_task表可能不存在:', e);
			}
			
			return {
				// 常规项目
				projectCount: projectResult.deleted || 0,
				projectExtCount: projectExtResult.deleted || 0,
				
				// 研发项目
				researchProjectCount: researchProjectResult.deleted || 0,
				resourceCount: resourceResult.deleted || 0,
				historyCount: historyResult.deleted || 0,
				branchCount: branchResult.deleted || 0,
				versionCount: versionResult.deleted || 0,
				snapshotCount: snapshotResult.deleted || 0,
				
				// 任务
				taskCount: taskResult.deleted || 0
			};
		} catch (err) {
			console.error('[清理项目] 操作失败:', err);
			this.AppError('[清理项目] 操作失败:' + err.message);
		}
	}
}

module.exports = AdminProjectService; 